texture: Split out type detection
authorBenjamin Otte <otte@redhat.com>
Tue, 14 Sep 2021 15:03:49 +0000 (17:03 +0200)
committerBenjamin Otte <otte@redhat.com>
Thu, 16 Sep 2021 22:25:22 +0000 (00:25 +0200)
This way, the code using it becomes clearer and we can use it in
multiple places without accidentally doing it wrong (hint: see next
commit).

gdk/gdktexture.c
gdk/loaders/gdkjpegprivate.h
gdk/loaders/gdkpngprivate.h
gdk/loaders/gdktiffprivate.h

index 261652ec4103e1fabb326b4162b81e6ba9a74145..a33fc3347c7556fe710dafa9e7e398c7c757491d 100644 (file)
@@ -431,52 +431,38 @@ GdkTexture *
 gdk_texture_new_from_bytes (GBytes  *bytes,
                             GError **error)
 {
-  const char *data;
-  gsize size;
-
   g_return_val_if_fail (bytes != NULL, NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  data = g_bytes_get_data (bytes, &size);
-
-  if (size > strlen (PNG_SIGNATURE) &&
-      memcmp (data, PNG_SIGNATURE, strlen (PNG_SIGNATURE)) == 0)
+  if (gdk_is_png (bytes))
     {
       return gdk_load_png (bytes, error);
     }
-  else if ((size > strlen (TIFF_SIGNATURE1) &&
-            memcmp (data, TIFF_SIGNATURE1, strlen (TIFF_SIGNATURE1)) == 0) ||
-           (size > strlen (TIFF_SIGNATURE2) &&
-            memcmp (data, TIFF_SIGNATURE2, strlen (TIFF_SIGNATURE2)) == 0))
+  else if (gdk_is_jpeg (bytes))
     {
-      return gdk_load_tiff (bytes, error);
+      return gdk_load_jpeg (bytes, error);
     }
-  else if (size > strlen (JPEG_SIGNATURE) &&
-           memcmp (data, JPEG_SIGNATURE, strlen (JPEG_SIGNATURE)) == 0)
+  else if (gdk_is_tiff (bytes))
     {
-      return gdk_load_jpeg (bytes, error);
+      return gdk_load_tiff (bytes, error);
     }
   else
     {
       GInputStream *stream;
       GdkPixbuf *pixbuf;
+      GdkTexture *texture;
 
       stream = g_memory_input_stream_new_from_bytes (bytes);
       pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
       g_object_unref (stream);
+      if (pixbuf == NULL)
+        return NULL;
 
-      if (pixbuf)
-        {
-          GdkTexture *texture;
-
-          texture = gdk_texture_new_for_pixbuf (pixbuf);
-          g_object_unref (pixbuf);
+      texture = gdk_texture_new_for_pixbuf (pixbuf);
+      g_object_unref (pixbuf);
 
-          return texture;
-        }
+      return texture;
     }
-
-  return NULL;
 }
 
 /**
index a8e6bd8a82e01e3f5ae8e5d1a2e42bff0b71c7c0..4dcccbaf8d08e569068c792e0179d91121186eea 100644 (file)
 GdkTexture *gdk_load_jpeg         (GBytes           *bytes,
                                    GError          **error);
 
+static inline gboolean
+gdk_is_jpeg (GBytes *bytes)
+{
+  const char *data;
+  gsize size;
+
+  data = g_bytes_get_data (bytes, &size);
+
+  return size > strlen (JPEG_SIGNATURE) &&
+         memcmp (data, JPEG_SIGNATURE, strlen (JPEG_SIGNATURE)) == 0;
+}
+
 #endif
index ca824579a2f4e0b8c04e3c4d9fd4837e75a2d661..cbe073b31592dbfc405ec6847ef9310cefd24932 100644 (file)
@@ -28,4 +28,16 @@ GdkTexture *gdk_load_png        (GBytes         *bytes,
 
 GBytes     *gdk_save_png        (GdkTexture     *texture);
 
+static inline gboolean
+gdk_is_png (GBytes *bytes)
+{
+  const char *data;
+  gsize size;
+
+  data = g_bytes_get_data (bytes, &size);
+
+  return size > strlen (PNG_SIGNATURE) &&
+         memcmp (data, PNG_SIGNATURE, strlen (PNG_SIGNATURE)) == 0;
+}
+
 #endif
index ee97884e75a1d14f95a9e99b73a4c08375bdc959..839e8855f0e0cced4ca9dc1a8353711a06739180 100644 (file)
@@ -29,4 +29,18 @@ GdkTexture *gdk_load_tiff         (GBytes           *bytes,
 
 GBytes *    gdk_save_tiff         (GdkTexture       *texture);
 
+static inline gboolean
+gdk_is_tiff (GBytes *bytes)
+{
+  const char *data;
+  gsize size;
+
+  data = g_bytes_get_data (bytes, &size);
+
+  return (size > strlen (TIFF_SIGNATURE1) &&
+          memcmp (data, TIFF_SIGNATURE1, strlen (TIFF_SIGNATURE1)) == 0) ||
+         (size > strlen (TIFF_SIGNATURE2) &&
+          memcmp (data, TIFF_SIGNATURE2, strlen (TIFF_SIGNATURE2)) == 0);
+}
+
 #endif